//
//  NewFeatureController.m
//  ETong
//
//  Created by wanzhao on 15/10/28.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "NewFeatureController.h"
#define IMGCOUNT 4
#import "LoginViewController.h"
#import "WZNavigationController.h"
#import "WZRootViewController.h"
#import "CAccountMgr.h"
#import "CAccountModel.h"
#import "CFolderMgr.h"
#import "CMessageMgr.h"
#import "CXmppMgr.h"
#import "EFengAndEtongRootController.h"
#import "EFRootViewController.h"
#import "PersonalVersion.h"

@interface NewFeatureController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, weak) UIButton *beginBtn;

@end

@implementation NewFeatureController


- (void)dealloc
{
    NSLog(@"NewFeatureController   dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupScrollView];
    [self setupPagecontrol];
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    scrollView.scrollEnabled = YES;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    UIImageView *bg = [[UIImageView alloc] init];
    bg.userInteractionEnabled = YES;
    bg.backgroundColor = ClearColor;
//    bg.image = [UIImage imageNamed:@"bg_newFeafuture"];
    bg.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH * IMGCOUNT, HARDWARE_SCREEN_HEIGHT);
    [self.scrollView addSubview:bg];

    
    CGFloat x = 0;
    UIImageView *beginImgView = nil;
    for (int i = 0; i < IMGCOUNT; i ++) {
        UIImageView *imgView = [[UIImageView alloc] init];
        x = i * HARDWARE_SCREEN_WIDTH;
        imgView.frame = CGRectMake(x, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT);
        imgView.backgroundColor = ClearColor;
        imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"content%d",i + 1]];
        [bg addSubview:imgView];
        if (i == IMGCOUNT - 1) {
            beginImgView = imgView;
        }
    }

    beginImgView.userInteractionEnabled = YES;
    UIButton *beginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    beginBtn.backgroundColor = ClearColor;
    [beginBtn setImage:[UIImage imageNamed:@"experience_btn_normal"] forState:UIControlStateNormal];
//    [beginBtn setImage:[UIImage imageNamed:@"experience_btn_pressed"] forState:UIControlStateHighlighted];
    [beginImgView addSubview:beginBtn];
    [beginBtn addTarget:self action:@selector(beginExperience) forControlEvents:UIControlEventTouchUpInside];
    //    [beginBtn setTitle:@"开始体验" forState:UIControlStateNormal];
    self.beginBtn = beginBtn;
    
}

- (void)beginExperience
{
    UIApplication *app = [UIApplication sharedApplication];
    app.statusBarHidden = NO;
    app.statusBarStyle = UIStatusBarStyleLightContent;
    LoginViewController *login = [[LoginViewController alloc] init];
    WZNavigationController *nav = [[WZNavigationController alloc] initWithRootViewController:login];
    nav.navigationBarHidden = YES;
    app.keyWindow.rootViewController = nav;
    
    CAccountModel *currentAcc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    BOOL isForceQuit = [USER_DEFAULT boolForKey:ISFORCEQUIT];
    
    nav.navigationBarHidden = YES;
    if(currentAcc != nil && currentAcc.userName.length > 0 && !isForceQuit)
    {
        [[FMShareDataBase shareDataBase] updatePersonalQueue];
        [[FMShareDataBase shareDataBase] updateCompanyQueue];
        PersonalVersion *persionVersionUpdate = [[PersonalVersion alloc] init];
        [persionVersionUpdate updateDBVersions:currentAcc.userName];

        CMessageMgr *mesMgr = [[CMessageMgr alloc] init];
        [mesMgr batchUpdateStatuWithIndeList:[mesMgr updateMessageStatuIsSendingToFailed]];
        [[CXmppMgr shareInstance] disconnect];
        [[CXmppMgr shareInstance] connect];
        if ([APPTOKEN isEqualToString:@"eFeng"]) {
            EFRootViewController *eFeng = [[EFRootViewController alloc] init];
            [login.navigationController pushViewController:eFeng animated:NO];
        } else if ([APPTOKEN isEqualToString:@"eTong"]){
            WZRootViewController *root = [[WZRootViewController alloc] init];
            [login.navigationController pushViewController:root animated:NO];
        } else if ([APPTOKEN isEqualToString:@"eFengAndEtong"]) {
            EFengAndEtongRootController *efengAndEtong = [[EFengAndEtongRootController alloc] init];
            [login.navigationController pushViewController:efengAndEtong animated:NO];
        } else {
            EFRootViewController *eFeng = [[EFRootViewController alloc] init];
            [login.navigationController pushViewController:eFeng animated:NO];
        }
    }
}
- (void)setupPagecontrol
{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    pageControl.numberOfPages = IMGCOUNT;
    [pageControl setValue:[UIImage imageNamed:@"default"] forKeyPath:@"_pageImage"];
    [pageControl setValue:[UIImage imageNamed:@"click"] forKeyPath:@"_currentPageImage"];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > HARDWARE_SCREEN_WIDTH * (IMGCOUNT - 1)) {
        [self beginExperience];
        
    }
    int currentPage = (scrollView.contentOffset.x + HARDWARE_SCREEN_WIDTH * 0.5) / HARDWARE_SCREEN_WIDTH;
    self.pageControl.currentPage = currentPage;
//    if (currentPage == IMGCOUNT - 1) {
//        self.pageControl.hidden = YES;
//    } else {
//        self.pageControl.hidden = NO;
//    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.scrollView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT);
    self.scrollView.contentSize = CGSizeMake(HARDWARE_SCREEN_WIDTH * IMGCOUNT + 1.0f, 0);
    
    
    //    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
    
    self.beginBtn.size = self.beginBtn.currentImage.size;
    self.beginBtn.centerX = self.view.centerX;
    self.beginBtn.y = HARDWARE_SCREEN_HEIGHT -  100 * HARDWARE_SCREEN_HEIGHT / 568.0;
    
    
    self.pageControl.centerX = self.view.centerX;
    self.pageControl.centerY = HARDWARE_SCREEN_HEIGHT -  45 * HARDWARE_SCREEN_HEIGHT / 568.0;
}


@end
